<?php

namespace App\Http\Controllers\Web;

use App\Models\Shop;
use Illuminate\Http\Request;

/**
 * Class ShopController
 * @package App\Http\Controllers\Web
 */
class ShopController extends Controller
{
    public function index(){
        $query = $this->query;
        $keywords = trim($query["keywords"]);

        $builder = Shop::query();

        unset($query["keywords"]);

        if ($keywords) {
            $builder->where(function ($query) use ($keywords) {
                $query->orWhere("name", "like", $keywords);
                $query->orWhere("realname", "like", $keywords);
                $query->orWhere("phone", "like", $keywords);
            });
        }

        $pager = $builder->orderBy('id','desc')->paginate($this->pageNum);

        return view("shop.index", ["pager" => $pager]);

    }

    public function create()
    {
        return view("shop.create");
    }


    /**
     * 保存多商户信息
     * @param Request $request
     * @return array|string
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $data["join_time"] = time();

        $model = new Shop();
        $model->fill($data)->save();

        return $this->success('', 'ok', route('shop.index'));
    }

    public function edit($id)
    {
        $model = Shop::find($id);
        return view("shop.edit", ["model" => $model]);
    }

    public function update(Request $request, $id)
    {
        $data = $request->all();
        $model = Shop::find($id);
        $model->fill($data)->save();

        return $this->success('','ok', route('shop.index'));
    }

    public function destroy($id)
    {
        $model = Shop::find($id);
        $model->delete();
        return $this->success('','ok', route('shop.index'));
    }

}